<?php

    function getClinicalVarOptions(){
        $query = "SELECT c.column_name FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.table_name = 'clinical_data'  ORDER BY c.column_name";
        return $query;
    }

    function getTissueTypeOptions () {
        $query = "SELECT DISTINCT name FROM tissue_type";
        return $query;
    }

    function getSNPGenoProExpl($snp, $probe_id, $tissue_type) {
        $query = "SELECT t1.snp, t1.genotype, t2.probe_id, t2.exp_level, t1.ind_nbr FROM genotype AS t1 INNER JOIN exp_level AS t2 ON t1.ind_nbr = t2.ind_nbr AND t1.snp = '$snp' AND t2.probe_id = '$probe_id' AND tissue_type = '$tissue_type'";
        return $query;
    }

    function getGeneSymFromProbeId($probe_id) {
        $query = "SELECT DISTINCT gene_sym FROM gene_names_symbols WHERE probe_id = '$probe_id'";
        return $query;
    }

    function getProbeIdFromGeneSymbol($genesym) {
        $query = "SELECT DISTINCT probe_id FROM gene_names_symbols WHERE gene_sym = '$genesym'";
        return $query;
    }

    function getGenotype($snp) {
        $query = "SELECT genotype FROM genotype WHERE snp='$snp'";
        return $query;
    }

    function getExpressionLevel($probe_id) {
        $query = "SELECT exp_level FROM exp_level WHERE probe_id='$probe_id'";
        return $query;
    }

    function getAnnotation($probe_id) {
        $query = "SELECT probe_id, gene_sym, gene_name FROM gene_names_symbols where probe_id='$probe_id'";
        return $query;
    }

    function getCLINvarProExpl($clin_var, $probe_id, $tissue_type) {
        $query = "SELECT t1.`$clin_var`, t1.ind_nbr, t2.exp_level, t2.ind_nbr FROM clinical_data AS t1 INNER JOIN exp_level AS t2 ON t1.ind_nbr = t2.ind_nbr AND t2.probe_id = '$probe_id' AND tissue_type = '$tissue_type'";
        return $query;
    }

    function getGeneVsGeneJoin($probe_id1, $probe_id2, $tissue_type) {
        $query = "SELECT t1.exp_level as exp1, t2.exp_level as exp2, t1.ind_nbr FROM exp_level AS t1 INNER JOIN exp_level AS t2 ON t1.ind_nbr = t2.ind_nbr AND t1.probe_id = '$probe_id1' AND t2.probe_id = '$probe_id2' AND t1.tissue_type = '$tissue_type' AND t2.tissue_type = '$tissue_type'";
        return $query;

    }

    function getAllGeneSymbols() {
        $query = "select distinct gene_sym from gene_names_symbols";
        return $query;
    }
    
    function matchSNP ($snp) {
        $query = "select distinct snp from genotype where snp='$snp'";
        return $query;
    }
    
    function checkGeneSyms($input_gene_sym) {
        $input_gene_sym_copy = array();
        /*
         * Remove trailing whitespaces from the gene symbol inputs
         */
        foreach ($input_gene_sym as $gsym) {
            array_push($input_gene_sym_copy,trim($gsym));
        }

        // Check through all gene symbols in the gene_names_symbol table,
        // and then remove every gene name that is found from the $input_gene_sym_copy.
        // If this is not empty after looping through the results from the table,
        // it means that the those gene names not removed are not present in the table.

        $all_gene_symbols = mysql_query(getAllGeneSymbols());

        while ($geneSymRow = mysql_fetch_row($all_gene_symbols)) {
           foreach ($input_gene_sym_copy as $current_gene_sym) {
                if (preg_match("/^$geneSymRow[0]$/", $current_gene_sym) == 1) {
                    /* Remove the matched value from the array */
                    $key = array_search($current_gene_sym, $input_gene_sym_copy);
                    unset($input_gene_sym_copy[$key]);
                    $input_gene_sym_copy = array_values($input_gene_sym_copy);
                }
           }
        }

        /* Look at array length after the check, should be 0 if all input gene symbols matched */
        if (count($input_gene_sym_copy) != 0) {
            /* Abort program and show which genesymbol(s) were incorrect */
            $error_message = "Unrecognized gene symbol input: <br>";
            foreach ($input_gene_sym_copy as $gene_name)
                    {
                            $error_message .= $gene_name . "<br>";
                    }

            $error_message .= "<br>";
            exit($error_message);
        }
    }
    
    function checkSNPid ($input_snp) {
        $input_snp_copy = array();
        /*
         * Remove trailing whitespaces from the gene symbol inputs
         */
        foreach ($input_snp as $snp) {
            array_push($input_snp_copy,trim($snp));
        }

        // Check through all gene symbols in the gene_names_symbol table,
        // and then remove every gene name that is found from the $input_gene_sym_copy.
        // If this is not empty after looping through the results from the table,
        // it means that the those gene names not removed are not present in the table.

        
        foreach ($input_snp_copy as $current_snp) {
            $snp_check = mysql_query(matchSNP($current_snp));
            while ($snp_row = mysql_fetch_row($snp_check)) {
                if (preg_match("/^$snp_row[0]$/", $current_snp) == 1) {
                    /* Remove the matched value from the array */
                    $key = array_search($current_snp, $input_snp_copy);
                    unset($input_snp_copy[$key]);
                    $input_snp_copy = array_values($input_snp_copy);
                }
            }
        }

        /* Look at array length after the check, should be 0 if all input gene symbols matched */
        if (count($input_snp_copy) != 0) {
            /* Abort program and show which genesymbol(s) were incorrect */
            $error_message = "Unrecognized SNP ID input: <br>";
            foreach ($input_snp_copy as $snp_name)
                    {
                            $error_message .= $snp_name . "<br>";
                    }

            $error_message .= "<br>";
            exit($error_message);
        }
        
    }
    
    function removeEmptyRows ($input_array) {
        $input_array_no_empties = array();
        foreach ($input_array as $input) {
            if (strlen(trim($input)) != 0) {
                // Element is not empty
                array_push($input_array_no_empties, $input);                               
            }
        }
        return $input_array_no_empties;
    }
  ?>
